<template>
  <div class="content">
    <el-dialog title="科目管理" :visible.sync="dialogVisible" width="450px" @close="closeDialog">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px"  style="width: 100%" class="demo-ruleForm">
        <el-form-item label="科目名称" prop="name">
          <el-input type="name" v-model="form.name" placeholder="例如：语文" ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleUpdate(form)" v-if="this.form.id">更新</el-button>
          <el-button type="primary" @click="handleSave(form)" v-else>添加</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-form :inline="true"  class="demo-form-inline">
      <el-form-item>
        <el-input  placeholder="科目名称" v-model="name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button  @click="getList()">搜索</el-button>
      </el-form-item>
      <el-form-item >
        <el-form-item >
          <el-popconfirm title="这是确定批量删除吗？" @onConfirm="handleDel(null)">
            <template #reference>
              <el-button type="danger" slot="reference" :disabled="delBtlStatu" >批量删除</el-button>
            </template>
          </el-popconfirm>
        </el-form-item>
      </el-form-item>
      <el-form-item >
        <el-button  @click="dialogVisible = true" type="primary">增加科目</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="table"
      ref="multipleTable"
      border
      stripe
      style="width: 100%"
      v-loading="loading"
      element-loading-text="拼命加载中..."
      element-loading-background="rgba(255, 255, 255, 0.7)"
      @row-click="toggleSelection"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column
        prop="id"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="name"
        label="科目名称">
      </el-table-column>
      <el-table-column
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button slot="reference" type="text" size="small" @click="handelEdit(scope.row)">编辑</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm title="这是确定删除吗？" @onConfirm="handleDel(scope.row.id)">
            <el-button type="text" size="small" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="current"
      :page-sizes="[5, 10, 20, 30]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { del, getList, save, update } from '@/api/sysEdu/subject'

export default {
  name: "Subject",
  data() {
    return {
      table:[],
      form:{
        id:"",
        name:"",
      },
      loading: true,
      // 分页用到的属性
      total: 0,
      current: 1,
      size: 10,
      delBtlStatu: true,
      name:"",
      // 根据多选的数量，将其批量按钮可用/不可用进行变化
      multipleSelection: [],
      dialogVisible: false,
      //校验
      rules: {
        name: [
          { required: true, message: '请输入科目名称', trigger: 'blur' },
        ],
      },

    }
  },
  created() {
    this.getList()
  },
  methods: {
    toggleSelection(row) {
      this.$refs.multipleTable.toggleRowSelection(row);
    },
    // 触发事件为有复选框发生变化，会传递所有勾选的行数据对象数组
    handleSelectionChange(val) {
      this.multipleSelection = val;
      //设置批量删除的按钮的可用/不可用
      this.delBtlStatu = val.length === 0
    },
    //这里是分页
    handleSizeChange(val) {
      this.size = val
      this.getList()
      this.getClassList()
    },
    handleCurrentChange(val) {
      this.current = val
      this.getList()
      this.getClassList()
    },
    getList(){
      getList(this.name,this.current,this.size).then(res => {
        this.table = res.data.data.records
        this.size = res.data.data.size
        this.total = res.data.data.total
        this.current = res.data.data.current
        this.loading = false
      })
    },
    closeDialog(){
      this.form = { name:""}
      this.$refs.form.resetFields();
      this.dialogVisible = false
    },
    handleUpdate(){
      update(this.form).then(res => {
        this.$message({
          message:res.data.msg,
          type:"success"
        })
        this.getList()
        this.dialogVisible = false
      })
    },
    handleSave(){
      save(this.form).then(res =>{
        this.$message({
          message:res.data.msg,
          type:"success"
        })
        this.getList()
        this.dialogVisible = false
      })
    },
    handelEdit(row){
      this.dialogVisible = true
      this.$nextTick(() => {
        this.form = JSON.parse(JSON.stringify(row))
      })
    },
    handleDel(id){
      const ids = []
      if (id) {
        ids.push(id)
      }else {
        this.multipleSelection.forEach(row => {
          ids.push(row.id)
        })
      }

      del(ids).then(res => {
        this.$message({
          message: res.data.msg,
          type: 'success'
        });
        this.getList()
      })
    },
  }
}
</script>

<style scoped>

</style>
